package floappcontest.core.backbeans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import floappcontest.core.beans.Team;
import floappcontest.core.beans.Tutor;
import floappcontest.core.control.Control;
import floappcontest.core.dao.factory.HibernateDAOFactory;
import floappcontest.core.exception.FloAppException;
import floappcontest.util.converters.TutorConverter;

/**
 * The TeamBean is a BackBean (JavaBean) related with the 
 * team events (insert, delete, edit, etc) in the pages.  
 */
public class TeamBean {

	private static final String FAILED = "FAILED"; //$NON-NLS-1$

	private static final String OK = "OK"; //$NON-NLS-1$

	private Control control;
	
	private Team team;
	
	private List<String> students;
	
	private String studentName;
	
	private List<Team> list;
	
	private List<SelectItem> tutorList;

	private TutorConverter converter;


	
	/**
	 * 
	 */
	public TeamBean() {
		setControl( new Control( new HibernateDAOFactory() ) );
		if (getTeam() == null){
			setTeam( new Team() );
		}
		getTeam().setTutor( new Tutor() );
		setStudents( new ArrayList<String>() );
		refreshList();
		listTutors();
	}
	
	private void listTutors() {
		List<Tutor> tutors = getControl().listTutors();
		setTutorList( new ArrayList<SelectItem>() );
		for ( Tutor tutor : tutors ) {
			getTutorList().add( new SelectItem( tutor, tutor.getLogin()) );
		}
	}

	
	/**
	 * Returns the team
	 * @return the team
	 */
	public Team getTeam() {
		return this.team;
	}
	
	/**
	 * Setting the team
	 * @param team -  the team
	 */
	public void setTeam(Team team) {
		this.team = team;
	}

	/**
	 * Navigation handling method (Action methods) that insert a team. 
	 * @return "OK" if the insertion occur without problems, otherwise "FAILED".
	 */
	public String insertTeam() {
		try {
			System.out.println(getStudents());
			getControl().insertTeam( this.team );
			return OK;
		} catch (FloAppException e) {
			setTeam( new Team() );
			return FAILED;
		}finally{
			refreshList();
		}
	}
	
	/**
	 * @return
	 */
	public void refreshList(){		
		setList( getControl().listTeams() );		
	}
	
	/**
	 * @return
	 */
	public Control getControl() {
		return this.control;
	}

	/**
	 * @param control
	 */
	public void setControl(Control control) {
		this.control = control;
	}

	public List<Team> getList() {
		return this.list;
	}

	public void setList(List<Team> list) {
		this.list = list;
	}
	
	public String removeTeam(){
		try {
			getControl().removeTeam( getTeam().getName() );
			return new MenuBarRedirection().listTeamPage();
		} catch (FloAppException e) {
			e.printStackTrace();
			return FAILED;
		}
		
	}

	
	public List<String> getStudents() {
		return this.students;
	}

	
	public void setStudents( List<String> students ) {
		this.students = students;
	}

	
	public String getStudentName() {
		return this.studentName;
	}

	
	public void setStudentName( String studentName ) {
		this.studentName = studentName;
	}
	
	public List<SelectItem> getTutorList() {
		return this.tutorList;
	}

	
	public void setTutorList( List<SelectItem> tutorList ) {
		this.tutorList = tutorList;
	}

	
	public TutorConverter getConverter() {
		listTutors();
		this.converter = new TutorConverter( getTutorList() );
		return this.converter;
	}

	
	public void setConverter( TutorConverter converter ) {
		this.converter = converter;
	}

}
